package de.zainodis.triplog.ui;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import de.zainodis.triplog.R;
import de.zainodis.triplog.data.share.TriplogContract;


public class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new SettingsFragment())
                    .commit();
        }
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        private boolean initialLoading = true;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
            initialLoading = false;
        }

        @Override
        public void onResume() {
            super.onResume();
            getPreferenceScreen().getSharedPreferences()
                    .registerOnSharedPreferenceChangeListener(this);
            PreferenceManager manager = getPreferenceManager();
            SharedPreferences preferences = manager.getSharedPreferences();
            onSharedPreferenceChanged(preferences, getString(R.string.pref_key_weather_units));
        }

        @Override
        public void onPause() {
            super.onPause();
            // Unregister the listener whenever a key changes
            getPreferenceScreen().getSharedPreferences()
                    .unregisterOnSharedPreferenceChangeListener(this);
        }


        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Preference preference = findPreference(key);

            if (!initialLoading) {
                if (getString(R.string.pref_key_weather_units).equals(preference.getKey())) {
                    //The weather code may be impacted by other changes (not location)
                    getActivity().getContentResolver().notifyChange(TriplogContract.CONTENT_URI_WEATHER, null);
                }
            }

            if (preference instanceof ListPreference) {
                ListPreference listPref = (ListPreference) preference;
                CharSequence value = listPref.getValue();
                if (!TextUtils.isEmpty(value)) {
                    preference.setSummary(value);
                }
            } else if (preference instanceof EditTextPreference) {
                EditTextPreference editPref = (EditTextPreference) preference;
                CharSequence value = editPref.getText();
                if (!TextUtils.isEmpty(value)) {
                    preference.setSummary(value);
                }
            }
        }
    }
}
